# verison:python3.8
import codecs
import json
from scipy.spatial.distance import cdist
import time
from numpy import array as nparr
from numpy import arange as nprange
import psutil


def write_to_json(data, name):
    tmp_data = {}
    tmp_data["data"] = data
    tmp_json = json.dumps(tmp_data, ensure_ascii=False)
    with open(name, 'w+') as f:
        f.write(tmp_json)


def ReadJsonFromTaipei():
    FileName = "Taipei_all_private.json"

    with open(FileName, encoding='utf-8-sig', errors='ignore') as f:
        Datas = json.load(f, strict=False)
        print(len(Datas), type(Datas))
        f.close()
    return Datas


def return_tmp():
    streets = ReadJsonFromTaipei()['data']
    max_streets = len(streets)
    # print(type(streets),len(streets))
    # return 0
    index = 1
    new_data = list()
    new_append = new_data.append
    streets_insert = streets.insert
    streets_pop = streets.pop

    tmp_loop = []
    tmp_append = tmp_loop.append
    # tmp_loop_clear = tmp_loop.clear()

    while (1):
        if (index > max_streets - 1):
            break

        distance = cdist([streets[index]], [streets[index - 1]], metric='euclidean')
        # distance=cdist([streets[6]],nparr(streets[:6]),metric='euclidean')
        dist_list = list(distance.tolist()[0])
        # print(type(dist_list),len(dist_list),dist_list,streets[6:8],streets[:6])
        # break
        # len_dist_list = len(dist_list)
        if (0.0016 < dist_list[0] < 0.0026):
            tmp_append([streets[index], streets[index - 1], dist_list[0]])
            new_append(streets[index])

        # 新的起始点
        index += 1

    print(f"len(new_data):{len(new_data)}")
    write_to_json(tmp_loop, "tmp_16-26.json")
    write_to_json(new_data, "new_16-26.json")


if __name__ == "__main__":
    print("start_time: ", time.asctime(time.localtime(time.time())))
    return_tmp()

    end_time = time.time()
    print("end_time: ", time.asctime(time.localtime(time.time())))
